package com.FTP;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class ftpClient {
	private FTPClient ftp=null;
	private boolean binaryTransfer=true;
	private ServAdd addr= new ServAdd();;
	
	public ftpClient(boolean mode)
	{
		ftp = new FTPClient();
		ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
		binaryTransfer = mode;
	}

	public void setServerAddress(String IP)
	{
		
		addr.setIP(IP);
		addr.setPort(1198);
	}
	
	public boolean connect(ServAdd add)
	{
        try
        {
            int reply;
            ftp.connect(add.getIP(),add.getPort());
            System.out.println("Connected to " + add.getIP() + ".");

            // After connection attempt, you should check the reply code to verify
            // success.
            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply))
            {
                ftp.disconnect();
                System.err.println("FTP server refused connection.");
                return false;
            }
        }
        catch (IOException e)
        {
            if (ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (IOException f)
                {
                }
            }
            System.err.println("Could not connect to server.");
            e.printStackTrace();
            return false;
        }
		return true;
	}
	
	public boolean login(String username,String password)
	{
         try {
			if (!ftp.login(username, password))
			 {
			     ftp.logout();
			     return false;
			 }
			
	         if (binaryTransfer)
	              ftp.setFileType(FTP.BINARY_FILE_TYPE);
		} catch (IOException e) {
			e.printStackTrace();
		}
         // Use passive mode as default because most of us are
         // behind firewalls these days.
         ftp.enterLocalPassiveMode();
	            
		return true;
	}
	
	
	public void send(String localFile, String remoteFile)
	{
        OutputStream output;

        try {
			output = new FileOutputStream(localFile);
			ftp.retrieveFile(remoteFile, output);
			output.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void recv(String localFile, String remoteFile)
	{
        InputStream input;

        try {
			input = new FileInputStream(localFile);
	        ftp.storeFile(remoteFile, input);
	        input.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void disconnect()
	{
        if (ftp.isConnected())
        {
           try
           {
              ftp.disconnect();
           }
           catch (IOException f)
           {
                   // do nothing
           }
       }
    }

}
